<?php
    class NucleoGestor extends BaseModel
    {
        protected $nucleogestorid;
        protected $nome;
        protected $fantasia;
        protected $rua;
        protected $numero;
        protected $complemento;
        protected $bairro;
        protected $cep;
        protected $telefone1;
        protected $telefone2;        

        const tablename = "nucleogestor";
        const tablekey = "nucleogestorid";
        const tablesequence = "nucleogestor_nucleogestorid_seq";

        public function salvar()
        {
            $this->cep         = TUtils::CepToNum( $this->cep );
            $this->telefone1   = TUtils::FoneToNum( $this->telefone1 );
            $this->telefone2   = TUtils::FoneToNum( $this->telefone2 );
            
            parent::salvar();
        }
        
        public function verificarDependencias( $ids )
        {
            $idsSql = '';
            if (is_array ($ids)){
                foreach ($ids as $id){
                    $idsSql .= ', ' . $id;
                }
                $idsSql = substr( $idsSql, 1 );
            }
            elseif (is_numeric ( $ids)){
                $idsSql = $ids;
            }
            else {
                throw new Exception( 'ID inválido.' );
            }
            
            $lotacao = new Lotacao();
            $result = $lotacao->listar( array('precondicao' => $this->getTableKey() . ' IN (' . $idsSql . ')' ) );
            
            $operador = new Operador();
            $result2 = $operador->listar( array('precondicao' => $this->getTableKey() . ' IN (' . $idsSql . ')' ) );
            
            if (pg_num_rows ($result) > 0 OR pg_num_rows ($result2) > 0){
                if (is_array ($ids)){
                    throw new Exception( TMensagens::getChavesEstrangeiras() );
                }
                else {
                    throw new Exception( TMensagens::getChaveEstrangeira() );
                }
            }
        }
        
        public function excluir()
        {
            if ( $this->getTableKeyValue() ){
                $this->verificarDependencias ($this->getTableKeyValue());
                parent::excluir();
            }            
        }
        
        public function excluirGrupo ($ids)
        {
            $this->verificarDependencias($ids);
            parent::excluirGrupo ($ids);
        }
    }
?>